import java.util.HashMap;
import java.util.Map;

/**
 * @author 雷明浩
 * @date 2025/5/29 11:06
 */
public class CopyRandomList {
    // leetcode 随机链表的复制
    // https://leetcode.cn/problems/copy-list-with-random-pointer/?envType=study-plan-v2&envId=top-100-liked
    public Node copyRandomList(Node head) {
        Map<Node, Node> map = new HashMap<>();
        Node cur = head;
        while (cur != null) {
            Node newNode = new Node(cur.val);
            map.put(cur, newNode);
            cur = cur.next;
        }
        for (Map.Entry<Node, Node> entry : map.entrySet()) {
            Node curNode = entry.getKey();
            Node newNode = entry.getValue();
            newNode.next = map.get(curNode.next);
            newNode.random = map.get(curNode.random);
        }
        return map.get(head);
    }
}